package api

import (
	"github.com/EDDYCJY/go-gin-example/models"
	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/e"
	"github.com/gin-gonic/gin"
)

//镜像查询请求参数绑定
type ImageQueryReq struct {
	// 页索引
	Page uint32 `form:"page" binding:"gte=1" json:"page"`
	// 页大小
	PageSize   uint32 `form:"page_size" binding:"gte=1" json:"page_size"`
	MirrorType int    `form:"mirror_type" json:"mirror_type"` //镜像类似
	Keyword    string `form:"key_word"json:"keyword"`         //关键词
}

type ImageDetailReq struct {
	ImageId string `form:"image_id" binding:"required`
}

//镜像下载处理请求
type ImageDownloadHandler struct {
}

func NewImageDownloadHandler() *ImageDownloadHandler {
	return &ImageDownloadHandler{}
}

type ImageListRsp struct {
	TotalCount int64                   `json:"total"`
	ImageList  []*models.ImageFramwork `json:"images"`
}

func (i *ImageDownloadHandler) QueryList(c *gin.Context) {
	appG := app.Gin{C: c}
	var req ImageQueryReq
	if err := c.ShouldBind(&req); err != nil {
		appG.ResponseError(e.ERROR, "请求参数错误:"+err.Error())
		return
	}
	//调用数据模型查询
	imageFramsModels := models.NewImageFramwork()
	imageList, ferror := imageFramsModels.GetListPage(req.Page, req.PageSize, req.MirrorType, req.Keyword)
	total := imageFramsModels.GetToal(req.MirrorType, req.Keyword)
	imageRes := ImageListRsp{
		TotalCount: total,
		ImageList:  imageList,
	}
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
	} else {
		appG.ResponseSuccess(imageRes)
	}
}

func (i *ImageDownloadHandler) Detail(c *gin.Context) {
	appG := app.Gin{C: c}
	var req ImageDetailReq
	if err := c.ShouldBind(&req); err != nil {
		appG.ResponseError(e.ERROR, "请求参数错误:"+err.Error())
		return
	}
	//模型
	//调用数据模型查询
	imageFramsModels := models.NewImageFramwork()
	detail, ferror := imageFramsModels.GetDetailByImageId(req.ImageId)
	if ferror.ErrCode != e.SUCCESS {
		appG.ResponseError(ferror.ErrCode, ferror.ErrMsg)
	} else {
		appG.ResponseSuccess(detail)
	}
}

//查询返回关键词
func (i *ImageDownloadHandler) Keywords(context *gin.Context) {
	appG := app.Gin{C: context}
	keywords := []string{
		"OneFlow",
		"MXnet",
		"PyTorch",
		"TensorFlow",
		"飞浆PaddlePaddle",
		"OpenCV",
		"Python",
		"CUDA",
		"Katago",
	}
	appG.ResponseSuccess(keywords)
}
